/*
 * Copyright (C) 2012  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.remote.rsa.message.data;

import java.rmi.RemoteException;

import org.osgi.framework.Bundle;

/**
 * ResponseMessage : The response message to a remote invocation.
 * 
 * @author Romain DUBOIS
 */
public class ResponseMessage extends Message {

    /** Serial version */
    private static final long serialVersionUID = 1L;

    /** The exception flag */
    private final boolean mException;
    /** The remote exception to throw from caller side */
    private RemoteException mRemoteException;

    /**
     * Constructor
     * 
     * @param pId
     *            The invocation message ID
     * @param pResult
     *            The invocation result or exception
     * @param pException
     *            If the remote call ends with a exception
     */
    public ResponseMessage(final long pId, final Object pResult, final boolean pException) {
        super(pId, pResult);
        mException = pException;
    }

    /**
     * Constructor
     * 
     * @param pId
     *            The invocation message ID
     * @param pRemoteException
     *            The remote exception
     */
    public ResponseMessage(final long pId, final RemoteException pRemoteException) {
        super(pId, null);
        mRemoteException = pRemoteException;
        mException = true;
    }

    /**
     * Get the remote call result.
     * 
     * @param pBundle
     *            The calling bundle
     * @return The remote call result
     * @throws Throwable
     *             The remote call exception
     */
    public Object getResult(final Bundle pBundle) throws Throwable {
        if (mException) {
            throw (Throwable) getData(pBundle);
        }
        return getData(pBundle);
    }

    /**
     * Get the remote exception.
     * 
     * @return The remote exception
     */
    public RemoteException getRemoteException() {
        return mRemoteException;
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.remote.rsa.message.data.Message#toString()
     */
    @Override
    public String toString() {
        final StringBuilder lBuilder = new StringBuilder(super.toString());
        lBuilder.append('[');
        if (mRemoteException == null) {
            if (mException) {
                lBuilder.append('X');
            } else {
                lBuilder.append('O');
            }
        } else {
            lBuilder.append(mRemoteException.getClass().getSimpleName());
            lBuilder.append(':').append(mRemoteException.getMessage());
        }
        lBuilder.append(']');
        return lBuilder.toString();
    }
}
